<?php
/**
 * （Logic）解析移库报告
 */

namespace App\Logic\Amazon\Parse;

use App\Models\Amazon\FbaRemovalOrderDetail;
use App\Util\RExcel;
class ParseRemovalOrderDetailReport
{
    /**
     * 解析RemovalOrderDetail
     * @param $file
     * @param $account
     * @return int
     */
    public static function exeParse($file,$account){
        $lines = RExcel::importFile($file);
        $i = 0;
        $count = 0;
        $head = [];
        foreach($lines as $_str){
            $str = ParseReport::getReplaceStr($_str);
            if($i == 0){
                $head = ParseReport::getTableHeadKey($str,self::$_keys);
                if(empty($head))return 2;
            }else{
                $flag = self::_addRemovalOrderDetail($account->id,$head,$str);
                $flag && $count++;
            }
            $i++;
        }
        return $i==($count+1) ? 2 : 3;
    }

    private static $_keys = ['request-date',
                             'order-id',
                             'order-type',
                             'order-status',
                             'last-updated-date',
                             'sku',
                             'fnsku',
                             'disposition',
                             'requested-quantity',
                             'cancelled-quantity',
                             'disposed-quantity',
                             'shipped-quantity',
                             'in-process-quantity',
                             'removal-fee',
                             'currency'];
    /**
     * 添加RemovalShipment
     * @param $accountId
     * @param $head
     * @param $str
     * @return bool
     */
    private static function _addRemovalOrderDetail($accountId,$head,$str){
        if(empty($head))return true;
        if(!$str)return true;
        $temp = explode("\t",$str);
        $keys = self::$_keys;
        $flag = false;
        foreach($keys as $key){
            if(!isset($temp[$head[$key]])){
                $flag = true;
                break;
            }
        }
        if($flag)return true;
        $requestDate = date('Y-m-d H:i:s',strtotime($temp[$head['request-date']]) - date("Z"));
        $lastUpdatedDate = date('Y-m-d H:i:s',strtotime($temp[$head['last-updated-date']]) - date("Z"));
        $re = FbaRemovalOrderDetail::query()
            ->updateOrCreate(
                [
                    'account_id' => $accountId,
                    'order_id' => $temp[$head['order-id']],
                    'sku' => $temp[$head['sku']]
                ],
                [
                    'request_date' => $requestDate,
                    'order_type' => $temp[$head['order-type']],
                    'order_status' => $temp[$head['order-status']],
                    'last_updated_date' => $lastUpdatedDate,
                    'fnsku' => $temp[$head['fnsku']],
                    'disposition' => $temp[$head['disposition']],
                    'requested_quantity' => $temp[$head['requested-quantity']] ?: 0,
                    'cancelled_quantity' => $temp[$head['cancelled-quantity']] ?: 0,
                    'disposed_quantity' => $temp[$head['disposed-quantity']] ?: 0,
                    'shipped_quantity' => $temp[$head['shipped-quantity']] ?: 0,
                    'in_process_quantity' => $temp[$head['in-process-quantity']] ?: 0,
                    'removal_fee' => $temp[$head['removal-fee']] ?: 0,
                    'currency' => $temp[$head['currency']] ?: null,
                ]);
        return !$re ? false : true;
    }
}
